package com.herench.bunch.exam.convert;

import ca.uhn.hl7v2.model.Message;
import ca.uhn.hl7v2.model.v24.group.*;
import ca.uhn.hl7v2.model.v24.message.ORR_O02;
import ca.uhn.hl7v2.model.v24.message.ORU_R01;
import ca.uhn.hl7v2.model.v24.segment.*;
import com.herench.bunch.commons.support.RequestMessage;
import com.herench.bunch.commons.utils.IdGenerator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author luoxiaoming on 18-9-17 下午5:25
 * @email luoxiaoming@herench.com
 */


public class ExamReportConvert extends AbstractMessageConvert {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Override
    Map<String, Object> doToMessage(RequestMessage requestMessage) throws Exception {
        Map<String, Object> resMap = new HashMap<String, Object>();

        ORU_R01 hl7 = (ORU_R01) requestMessage.getHl7Message();
        MSH msh = hl7.getMSH();
        resMap.put("actionType",msh.getMsh15_AcceptAcknowledgmentType().getValue());
        ORU_R01_PATIENT_RESULT patientResult = hl7.getPATIENT_RESULT(0);
        ORU_R01_PATIENT patient = patientResult.getPATIENT();
        PID pid = patient.getPID();
        resMap.put("patientId",pid.getPid2_PatientID().getCx1_ID().getValue());
        resMap.put("patientName",pid.getPid5_PatientName(0).getXpn1_FamilyName().getFn1_Surname().getValue());
        resMap.put("namePin",pid.getPid5_PatientName(0).getXpn2_GivenName().getValue());
        resMap.put("birthday",pid.getPid7_DateTimeOfBirth().getTs1_TimeOfAnEvent().getValue());
        resMap.put("sex",pid.getPid8_AdministrativeSex().getValue());
        resMap.put("phone",pid.getPid13_PhoneNumberHome(0).getXtn1_9999999X99999CAnyText().getValue());

        ORU_R01_VISIT visit = patient.getVISIT();
        PV1 pv1 = visit.getPV1();

        resMap.put("patientClass",pv1.getPv12_PatientClass().getValue());
        resMap.put("deptName",pv1.getPv13_AssignedPatientLocation().getPl1_PointOfCare().getValue());
        resMap.put("visitId",pv1.getPv119_VisitNumber().getCx1_ID().getValue());
        resMap.put("adminDateTime",pv1.getPv144_AdmitDateTime().getTs1_TimeOfAnEvent().getValue());

        ORU_R01_ORDER_OBSERVATION orderObservation = patientResult.getORDER_OBSERVATION(0);
        OBR obr = orderObservation.getOBR();

        resMap.put("examNo",obr.getObr2_PlacerOrderNumber().getEi1_EntityIdentifier().getValue());
        resMap.put("itemNo",obr.getObr4_UniversalServiceIdentifier().getCe1_Identifier().getValue());
        resMap.put("itemName",obr.getObr4_UniversalServiceIdentifier().getCe2_Text().getValue());
        resMap.put("fee",obr.getObr23_ChargeToPractice().getMoc1_DollarAmount().getMo1_Quantity().getValue());
        resMap.put("technicianId",obr.getObr34_Technician(0).getNdl1_OPName().getCnn1_IDNumber().getValue());
        resMap.put("technicianName",obr.getObr34_Technician(0).getNdl1_OPName().getCnn2_FamilyName().getValue());
        resMap.put("transcriptionistId",obr.getObr35_Transcriptionist(0).getNdl1_OPName().getCnn1_IDNumber().getValue());
        resMap.put("transcriptionistName",obr.getObr35_Transcriptionist(0).getNdl1_OPName().getCnn2_FamilyName().getValue());

        ORU_R01_OBSERVATION observation = orderObservation.getOBSERVATION(0);
        OBX obx = observation.getOBX();
        ORU_R01_OBSERVATION observation1 = orderObservation.getOBSERVATION(1);
        OBX obx1 = observation1.getOBX();

        resMap.put("setId",obx.getObx1_SetIDOBX().getValue());
        resMap.put("reportType",obx.getObx2_ValueType().getValue());
        resMap.put("observation ",obx.getObx3_ObservationIdentifier().getCe2_Text().getValue());
        resMap.put("message",obx.getObx3_ObservationIdentifier().getCe3_NameOfCodingSystem().getValue());
        resMap.put("flag",obx.getObx8_AbnormalFlags().getValue());
        resMap.put("reportStatus",obx.getObx11_ObservationResultStatus().getValue());
        resMap.put("verifiedDoctorNo",obx.getObx16_ResponsibleObserver().getXcn1_IDNumber().getValue());
        resMap.put("verifiedDoctorName",obx.getObx16_ResponsibleObserver().getXcn2_FamilyName().getFn1_Surname().getValue());
        resMap.put("url",obx.getObx17_ObservationMethod(0).getCe1_Identifier().getValue());

        resMap.put("setId1",obx1.getObx1_SetIDOBX().getValue());
        resMap.put("reportType1",obx1.getObx2_ValueType().getValue());
        resMap.put("observation1",obx1.getObx3_ObservationIdentifier().getCe2_Text().getValue());
        resMap.put("message1",obx1.getObx3_ObservationIdentifier().getCe3_NameOfCodingSystem().getValue());
        resMap.put("reportStatus1",obx1.getObx11_ObservationResultStatus().getValue());;

        logger.info(requestMessage.getMsgCode() + " params :"+ resMap);
        return resMap;
    }

    @Override
    public Message fromMessage(Object message) throws Exception {
//        Assert.notNull(message, "message 为空.");
        Map<String, Object> res = (Map<String, Object>) message;
        ORR_O02 orrO02 = new ORR_O02();
        orrO02.initQuickstart("ORR_O02", "ORR_O02", "P");
        MSH msh = orrO02.getMSH();
        initResponseMessageMSH(msh);
        MSA msa = orrO02.getMSA();
        String ret = (String) res.get("ret");
//        if ("0".equals(ret)){
        msa.getErrorCondition().getCe1_Identifier().setValue("0");
//            msa.getErrorCondition().getCe2_Text().setValue("操作成功,调用HIS接口返回:ret:"+ret);
        msa.getErrorCondition().getCe2_Text().setValue("操作成功");
//        }else {
//            msa.getErrorCondition().getCe1_Identifier().setValue("-1");
//            msa.getErrorCondition().getCe2_Text().setValue("操作失败,调用HIS接口返回:ret:"+ret);
//        }
        return orrO02;
    }

    @Override
    public Message generatorReqMessage() throws Exception {
        ORU_R01 message = new ORU_R01();
        message.initQuickstart("ORU_R01", "ORU_R01", "R");
        MSH msh = message.getMSH();
        msh.getMsh11_ProcessingID().getPt1_ProcessingID().setValue("900010");
        msh.getMsh11_ProcessingID().getPt2_ProcessingMode().setValue(IdGenerator.getUUID());
        msh.getMsh3_SendingApplication().getHd1_NamespaceID().setValue("LIS");
        msh.getMsh7_DateTimeOfMessage().getTs1_TimeOfAnEvent().setValue(new Date());
        msh.getMsh10_MessageControlID().setValue(IdGenerator.getUUID());



        ORU_R01_PATIENT_RESULT patient_result = message.insertPATIENT_RESULT(0);

        ORU_R01_ORDER_OBSERVATION op =  patient_result.insertORDER_OBSERVATION(0);
        CTI cti = op.insertCTI(0);

        return null;
    }
}
